    import { getSubCategory, getCategoryFilter } from '@/network/category';
    import {ref, onMounted} from 'vue'
    import {useRoute} from 'vue-router'

export function useGoodList() {
    // 筛选商品数据
    const route = useRoute()
    const goodList = ref([])
    const data = ref({
        categoryId: route.params.id,
        page: 1,
        pageSize: 20,
        sortField: 'publishTime'
    })
    const getGoodList = async() => {
        const res = await getCategoryFilter(data.value)
        // console.log(res);
        goodList.value = res.result.items
    }

    onMounted(() => {
        getGoodList()
    })

    // tab切换
    function tabChange() {
        console.log('tab切换了', data.value.sortField);
        // 切换之后去请求不同数据
        data.value.page = 1 // 重置页数
        getGoodList()
    }

    // 加载更多
    const disabled = ref(false)
    const load = async () => {
        console.log('加载更多');
        // 获取下一页的数据
        data.value.page++
        const res = await getCategoryFilter(data.value)
        goodList.value = [...goodList.value, ...res.result.items]
        // 加载完毕，停止监听
        if(res.result.items.length === 0) {
            disabled.value = true
        }
    }

    return {
        goodList, 
        tabChange, 
        load,
        data, 
        disabled
    }
}